﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using DatabaseExtension.ConnectionString;
using DatabaseExtension;

namespace SqlCeProvider.Bulk
{
    public sealed class SqlCeBulkLoader : IDisposable
    {
        /// <summary>
        /// This integer value specifies the number of rows that should be copied in each 
        /// attempt to copy to database. This value has direct effect on the number of accesses to database. 
        /// </summary>
        public int BatchSize { get; set; }
        /// <summary>
        /// Column Mapping Collection.
        /// </summary>
        public HashSet<System.Data.SqlClient.SqlBulkCopyColumnMapping> ColumnMappings { get; private set; }
        /// <summary>
        /// Destination table name.
        /// </summary>
        public String DestinationTableName;
        /// <summary>
        /// Get or Set row Terminator
        /// Default Value: "\r\n"
        /// </summary>
        public String RowTerminator;
        /// <summary>
        /// Get or Set Column Terminator
        /// Default Value: ";"
        /// </summary>
        public String ColumnTerminator;
        /// <summary>
        /// Get or Set the file name where from to load.
        /// </summary>
        public String FileName;
        /// <summary>
        /// Get or Set the number of lines to skip for the Bulk Copy.
        /// </summary>
        public int NumberOfLinesToSkip;
        /// <summary>
        /// Get or Set File Encoding.
        /// Default Value: UTF8
        /// </summary>
        public Encoding FileEncoding;
        /// <summary>
        /// Bulk Copy Timeout. If value is 0 then start bcp without timeout.
        /// </summary>
        public int Timeout;
        /// <summary>
        /// Using Database Interface for Batching the data.
        /// </summary>
        private DbInterface DatabaseInterface;
        /// <summary>
        /// Create new SQL Compact Edition Bulk.
        /// </summary>
        /// <param name="dbi">Database Interface.</param>
        public SqlCeBulkLoader(DbInterface dbi)
        {
            this.DatabaseInterface = dbi;
            this.RowTerminator = "\r\n";
            this.ColumnTerminator = ";";
            this.FileEncoding = Encoding.UTF8;
        }
        /// <summary>
        /// Create new Bulk Copy from Connection Info.
        /// </summary>
        /// <param name="connectionInfo">Connection Information that provide all needed information.</param>
        public SqlCeBulkLoader(SqlCeConnectionInfo connectionInfo)
            : this(new DbInterface(connectionInfo))
        {
        }

        public void WriteToServer(System.Data.DataRow[] rows)
        {
        }
        public void WriteToServer(System.Data.DataTable table)
        {
        }
        public void WriteToServer(System.Data.IDataReader reader)
        {
        }
        /// <summary>
        /// Load Data from file.
        /// </summary>
        /// <returns>Number of rows loaded.</returns>
        public int Load()
        {
            if( String.IsNullOrEmpty(this.RowTerminator) == true || 
                String.IsNullOrEmpty(this.ColumnTerminator) == true)
                throw new ArgumentException();
            int rowTerminatorLength = this.RowTerminator.Length,
                result = 0,
                runner = 0;
            using (var sr = new System.IO.StreamReader(this.FileName, this.FileEncoding, true))
            {
                Char[] buffer = new Char[1024];
                while (sr.Read(buffer, 0, buffer.Length) != 0)
                {
                    if (buffer.Contains(this.RowTerminator[0]) == true)
                    {
                        var indexes = buffer.Select((a, i) => new { Index = i, Value = a }).Where(x => Char.Equals(x.Value, this.RowTerminator[0])).Select(a => a.Index);
                        while (rowTerminatorLength < runner++)
                        {

                        }
                    }
                }
            }
            return result;
        }

        #region Dispose
        /// <summary>
        /// Finalize Object
        /// </summary>
        ~SqlCeBulkLoader()
        {
            this.Dispose(false);
        }
        /// <summary>
        /// Dispose SqlCeBulkCopy
        /// </summary>
        public void Dispose()
        {
            this.Dispose(true);
            GC.SuppressFinalize(this);
        }
        private void Dispose(Boolean disposing)
        {
            if (disposing == true)
            {
                this.DatabaseInterface.Dispose();
            }
        }
        #endregion
    }
}
